<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" href="http://medoo.in/css/core.css"/>
<link rel="shortcut icon" href="http://medoo.in/favicon.png" />
<meta property="og:title" content="Medoo - The lightest PHP database framework to accelerate development"/>
<meta property="og:type" content="website"/>
<meta property="og:url" content="http://medoo.in"/>
<meta property="og:image" content="http://medoo.in/images/medoo-logo-128.png"/>
<meta property="og:site_name" content="Medoo"/>
<meta property="fb:admins" content="100001162628592" />
<script>(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObjects']=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)})(window,document,'script','//www.google-analytics.com/analytics.js','ga');ga('create', 'UA-15301273-5', 'medoo.in');ga('send', 'pageview');</script><title>Select API - Medoo</title>
<meta name="keywords" content="Medoo,Medoo API,select" />
<meta name="description" content="Medoo select API - Select data from database" />
<script type="text/javascript" src="../js/qatrix-1.1.js"></script>
<script type="text/javascript" src="../js/prettify.js"></script>
<script type="text/javascript">
$ready(function ()
{
	prettyPrint();
	$className.add($('api_list_select'), 'on');
});
</script>
</head>
<body>
<header>
<div id="header_body" class="clearfix">
<a href="http://medoo.in"><img width="297" height="58" id="head_logo" src="http://medoo.in/images/medoo-logo.png" /></a>
<nav>
	<ul class="clearfix">
		<li><a href="http://medoo.in">Home</a></li>
		<li><a href="http://medoo.in/api/new">Get Started</a></li>
		<li><a href="http://medoo.in/doc">Documentation</a></li>
		<li><a href="http://medoo.in/about">About</a></li>
	</ul>
</nav>
</div>
</header><div id="doc_container" class="clearfix">
<div id="api_list_wrap">
<h4>Get Started</h4 >
<ul class="api_list">
<li id="api_list_new"><a href="http://medoo.in/api/new">new medoo()</a></li>
</ul>

<h4>Where Syntax</h4 >
<ul class="api_list">
<li id="api_list_where"><a href="http://medoo.in/api/where">where</a></li>
</ul>

<h4>Query</h4>
<ul class="api_list">
<li id="api_list_select"><a href="http://medoo.in/api/select">select</a></li>
<li id="api_list_insert"><a href="http://medoo.in/api/insert">insert</a></li>
<li id="api_list_update"><a href="http://medoo.in/api/update">update</a></li>
<li id="api_list_delete"><a href="http://medoo.in/api/delete">delete</a></li>
<li id="api_list_replace"><a href="http://medoo.in/api/replace">replace</a></li>
<li id="api_list_get"><a href="http://medoo.in/api/get">get</a></li>
<li id="api_list_has"><a href="http://medoo.in/api/has">has</a></li>
<li id="api_list_count"><a href="http://medoo.in/api/count">count</a></li>
<li id="api_list_max"><a href="http://medoo.in/api/max">max</a></li>
<li id="api_list_min"><a href="http://medoo.in/api/min">min</a></li>
<li id="api_list_avg"><a href="http://medoo.in/api/avg">avg</a></li>
<li id="api_list_sum"><a href="http://medoo.in/api/sum">sum</a></li>
</ul>

<h4>Transaction</h4 >
<ul class="api_list">
<li id="api_list_action"><a href="http://medoo.in/api/action">action</a></li>
</ul>

<h4>Advanced</h4>
<ul class="api_list">
<li id="api_list_query"><a href="http://medoo.in/api/query">query</a></li>
<li id="api_list_quote"><a href="http://medoo.in/api/quote">quote</a></li>
</ul>

<h4>PDO object</h4>
<ul class="api_list">
<li id="api_list_pdo"><a href="http://medoo.in/api/pdo">pdo</a></li>
</ul>

<h4>Debug</h4>
<ul class="api_list">
<li id="api_list_debug"><a href="http://medoo.in/api/debug">debug</a></li>
<li id="api_list_error"><a href="http://medoo.in/api/error">error</a></li>
<li id="api_list_log"><a href="http://medoo.in/api/log">log</a></li>
<li id="api_list_last_query"><a href="http://medoo.in/api/last_query">last_query</a></li>
</ul>

<h4>Information</h4>
<ul class="api_list">
<li id="api_list_info"><a href="http://medoo.in/api/info">info</a></li>
</ul>
</div><div id="main_body">
<div id="doc_version">version: 1.1.3</div>
<h3>select</h3>
<p class="desc">Select data from database</p><div class="function_name">select($table, $columns, $where)</div>
		<ul class="para_list"><li><h5>table [string]</h5><p>The table name.</p></li><li><h5>columns [string/array]</h5><p>The target columns of data will be fetched.</p></li><li><h5>where (optional) [array]</h5><p>The WHERE clause to filter records.</p></li></ul><div class="function_name">select($table, $join, $columns, $where)</div>
		<ul class="para_list"><li><h5>table [string]</h5><p>The table name.</p></li><li><h5>join [array]</h5><p>Table relativity for table joining. Ignore it if no table joining required.</p></li><li><h5>columns [string/array]</h5><p>The target columns of data will be fetched.</p></li><li><h5>where (optional) [array]</h5><p>The WHERE clause to filter records.</p></li></ul>
<div class="function_return">Return: [array]</div><div class="note">You can use "*" as columns parameter to fetch all columns, but to increase the performance, providing the target columns is much better.</div><pre class="prettyprint linenums">$datas = $database-&gt;select(&quot;account&quot;, [
	&quot;user_name&quot;,
	&quot;email&quot;
], [
	&quot;user_id[&gt;]&quot; =&gt; 100
]);

// $datas = array(
// 	[0] =&gt; array(
// 		&quot;user_name&quot; =&gt; &quot;foo&quot;,
// 		&quot;email&quot; =&gt; &quot;foo@bar.com&quot;
// 	),
// 	[1] =&gt; array(
// 		&quot;user_name&quot; =&gt; &quot;cat&quot;,
// 		&quot;email&quot; =&gt; &quot;cat@dog.com&quot;
// 	)
// )

foreach($datas as $data)
{
	echo &quot;user_name:&quot; . $data[&quot;user_name&quot;] . &quot; - email:&quot; . $data[&quot;email&quot;] . &quot;&lt;br/&gt;&quot;;
}

// Select all columns
$datas = $database-&gt;select(&quot;account&quot;, &quot;*&quot;);

// Select a column
$datas = $database-&gt;select(&quot;account&quot;, &quot;user_name&quot;);

// $datas = array(
// 	[0] =&gt; &quot;foo&quot;,
// 	[1] =&gt; &quot;cat&quot;
// )
</pre><h4>Table joining</h4><p class="intro">SQL JOIN clause can combine rows together between two table. Medoo provide simple syntax for JOIN clause.</p><pre class="prettyprint linenums">// [&gt;] == LEFT JOIN
// [&lt;] == RIGH JOIN
// [&lt;&gt;] == FULL JOIN
// [&gt;&lt;] == INNER JOIN

$database-&gt;select(&quot;post&quot;, [
	// Here is the table relativity argument that tells the relativity between the table you want to join.

	// The row author_id from table post is equal the row user_id from table account
	&quot;[&gt;]account&quot; =&gt; [&quot;author_id&quot; =&gt; &quot;user_id&quot;],

	// The row user_id from table post is equal the row user_id from table album.
	// This is a shortcut to declare the relativity if the row name are the same in both table.
	&quot;[&gt;]album&quot; =&gt; &quot;user_id&quot;,

	// [post.user_id is equal photo.user_id and post.avatar_id is equal photo.avatar_id]
	// Like above, there are two row or more are the same in both table.
	&quot;[&gt;]photo&quot; =&gt; [&quot;user_id&quot;, &quot;avatar_id&quot;],

	// If you want to join the same table with different value,
	// you have to assign the table with alias.
	&quot;[&gt;]account (replyer)&quot; =&gt; [&quot;replyer_id&quot; =&gt; &quot;user_id&quot;],

	// You can refer the previous joined table by adding the table name before the column.
	&quot;[&gt;]account&quot; =&gt; [&quot;author_id&quot; =&gt; &quot;user_id&quot;],
	&quot;[&gt;]album&quot; =&gt; [&quot;account.user_id&quot; =&gt; &quot;user_id&quot;],

	// Multiple condition
	&quot;[&gt;]account&quot; =&gt; [
		&quot;author_id&quot; =&gt; &quot;user_id&quot;,
		&quot;album.user_id&quot; =&gt; &quot;user_id&quot;
	]
], [
	&quot;post.post_id&quot;,
	&quot;post.title&quot;,
	&quot;account.user_id&quot;,
	&quot;account.city&quot;,
	&quot;replyer.user_id&quot;,
	&quot;replyer.city&quot;
], [
	&quot;post.user_id&quot; =&gt; 100,
	&quot;ORDER&quot; =&gt; &quot;post.post_id DESC&quot;,
	&quot;LIMIT&quot; =&gt; 50
]);

// SELECT
// 	`post`.`post_id`,
// 	`post`.`title`,
// 	`account`.`city`
// FROM `post`
// LEFT JOIN `account` ON `post`.`author_id` = `account`.`user_id`
// LEFT JOIN `album` USING (`user_id`)
// LEFT JOIN `photo` USING (`user_id`, `avatar_id`)
// WHERE
// 	`post`.`user_id` = 100
// ORDER BY `post`.`post_id` DESC
// LIMIT 50
</pre><h4>Data mapping</h4><p class="intro">Customize output data construction - The key name for wrapping data has no relation with columns itself and it is multidimensional.</p><pre class="prettyprint linenums">$data = $database-&gt;select(&quot;post&quot;, [
	&quot;[&gt;]account&quot; =&gt; [&quot;user_id&quot;]
], [
	&quot;post.post_id&quot;,
	&quot;post.content&quot;,

	&quot;userData&quot; =&gt; [
		&quot;account.user_id&quot;,
		&quot;account.email&quot;,

		&quot;meta&quot; =&gt; [
			&quot;account.location&quot;,
			&quot;account.gender&quot;
		]
	]
], [
	&quot;LIMIT&quot; =&gt; [0, 2]
]);

echo json_encode($data);

// Outputed data
[
	{
		post_id: &quot;1&quot;,
		content: &quot;Hello world!&quot;,
		userData: {
			user_id: &quot;1&quot;,
			email: &quot;foo@example.com&quot;,
			meta: {
				location: &quot;New York&quot;,
				gender: &quot;male&quot;
			}
		}
	},
	{
		post_id: &quot;2&quot;,
		content: &quot;Hey everyone&quot;,
		userData: {
			user_id: &quot;2&quot;,
			email: &quot;bar@example.com&quot;,
			meta: {
				location: &quot;London&quot;,
				gender: &quot;female&quot;
			}
		}
	}
]</pre><h4>Alias</h4><p class="intro">You can use alias as a new column or table name instead of original one. This is useful for table joining to prevent name conflict.</p><pre class="prettyprint linenums">$data = $database-&gt;select(&quot;account&quot;, [
	&quot;user_id&quot;,
	&quot;nickname(my_nickname)&quot;
], [
	&quot;LIMIT&quot; =&gt; 20
]);

// $data = array(
// 	[0] =&gt; array(
// 		&quot;user_id&quot; =&gt; &quot;1&quot;,
// 		&quot;my_nickname&quot; =&gt; &quot;foo&quot;
// 	),
// 	[1] =&gt; array(
// 		&quot;user_id&quot; =&gt; &quot;2&quot;,
// 		&quot;my_nickname&quot; =&gt; &quot;bar&quot;
// 	)
// )

$data = $database-&gt;select(&quot;post (content)&quot;, [
	&quot;[&gt;]account (user)&quot; =&gt; &quot;user_id&quot;,
], [
	&quot;content.user_id (author_id)&quot;,
	&quot;user.user_id&quot;
], [
	&quot;LIMIT&quot; =&gt; 20
]);

// SELECT
// 	&quot;content&quot;.&quot;user_id&quot; AS author_id,
// 	&quot;user&quot;.&quot;user_id&quot;
// FROM
// 	&quot;post&quot; AS &quot;content&quot;
// LEFT JOIN &quot;account&quot; AS &quot;user&quot; USING (&quot;user_id&quot;)
// LIMIT 2

// $data = array(
// 	[0] =&gt; array(
// 		&quot;author_id&quot; =&gt; &quot;1&quot;,
// 		&quot;user_id&quot; =&gt; &quot;321&quot;
// 	),
// 	[1] =&gt; array(
// 		&quot;author_id&quot; =&gt; &quot;2&quot;,
// 		&quot;user_id&quot; =&gt; &quot;322&quot;
// 	)
// )</pre>	</div>
</div>

<footer>
<div id="footer_body" class="clearfix">
<a id="github_link" href="https://github.com/catfan/Medoo">
<div id="github_text">Fork me on GitHub</div>
</a><p id="footer_text">The Medoo project, All Rights Reserved, 2017.</p>
</div>
</footer></body>
</html>